package com.tech.tool.utils;

import com.google.gson.*;
import com.tech.tool.dto.swagger.SwaggerPathPostParametersDTO;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
public class Json2OtherUtil {

    public static <T> T jsonName2DTO(JsonObject jsonObject, String name, Class<T> cls) {
        T dto = null;
        Set<Map.Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
        for (Map.Entry<String, JsonElement> jsonElementEntry: entrySet) {
            String key = jsonElementEntry.getKey();
            if (key.equalsIgnoreCase(name)) {
                dto = new Gson().fromJson(jsonElementEntry.getValue(), cls);
                break;
            }
        }
        return dto;
    }
    public static <T> T json2DTO(JsonObject jsonObject, Class<T> cls) {
        T dto = null;
        Set<Map.Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
        for (Map.Entry<String, JsonElement> jsonElementEntry: entrySet) {
            dto = new Gson().fromJson(jsonElementEntry.getValue(), cls);
            break;
        }
        return dto;
    }

    public static Set<Map.Entry<String, JsonElement>> json2Set(JsonObject jsonObject) {
        Set<Map.Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
        return entrySet;
    }

    public static <T> T element2DTO(JsonElement jsonElement) {

        T dto = null;
        JsonObject jsonObject = jsonElement.getAsJsonObject();
        Set<Map.Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
        Iterator<Map.Entry<String, JsonElement>> entryIterator = entrySet.iterator();
        while(entryIterator.hasNext()){
            Map.Entry<String, JsonElement> elementEntry = entryIterator.next();
            String method = elementEntry.getKey();
            if (method.equals("post")) {
                SwaggerPathPostParametersDTO post = new Gson().fromJson(elementEntry.getValue(), SwaggerPathPostParametersDTO.class);
                dto = (T) post;
            } else if (method.equals("get")) {
//              SwaggerPathPostParametersDTO get = new Gson().fromJson(elementEntry.getValue(), SwaggerPathGetParametersDTO.class);
//              dto = (T) get;
                log.info("get");
            }

        }
        return dto;
    }

    public static String elementGetKey(JsonElement jsonElement) {
        List<String> list = new ArrayList<>();
        JsonObject jsonObject = jsonElement.getAsJsonObject();
        Set<Map.Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
        Iterator<Map.Entry<String, JsonElement>> entryIterator = entrySet.iterator();
        while(entryIterator.hasNext()){
            Map.Entry<String, JsonElement> elementEntry = entryIterator.next();
            String method = elementEntry.getKey();
            list.add(method);
        }
        // TODO 一般只有一种请求方式
        return list.get(0);
    }

//    public static JsonObject composeBody(Map.Entry<String, JsonElement> elementEntry) {
//
//    }


}
